package tpo.wmb.provider.service.impl;

import com.github.pagehelper.PageInfo;
import dto.Dto;
import dto.ShowWares;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.BuyOrder;
import pojo.BuyReturn;
import pojo.DetailList;
import pojo.User;
import tpo.wmb.provider.dao.BuyReturnMapper;
import tpo.wmb.provider.dao.DetailListMapper;
import tpo.wmb.provider.dao.OrderMapper;
import tpo.wmb.provider.dao.UserMapper;
import tpo.wmb.provider.service.OrderService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private BuyReturnMapper buyReturnMapper;

    @Resource
    private DetailListMapper detailListMapper;

    @Resource
    private UserMapper userMapper;


    @Override
    public List<BuyOrder> selectOrderList(Map<String, Object> map) {
        return orderMapper.selectOrderList(map);
    }

    @Override
    public BuyOrder selectOrder(Integer id) {
        return orderMapper.selectOrder(id);
    }

    @Transactional //事务管理
    @Override // x修改订单,先删除订单明细, 再重新添加订单明细,再修改订单信息
    public Integer updateOrder(BuyOrder buyOrder) {
            detailListMapper.deleteDetailList2(buyOrder.getCode()); // 先删除这个订单的所有明细
            for (DetailList detailList : buyOrder.getList()) {//再重新添加修改的订单明细
                detailList.setOrderId(buyOrder.getCode());
                detailListMapper.insertDetailList(detailList);
            }
        return orderMapper.updateOrder(buyOrder);
    }


    @Override
    public Integer updateOrder2(BuyOrder buyOrder) {
        return orderMapper.updateOrder(buyOrder);
    }


    @Transactional //事务管理
    @Override //订单已收款,减去作废的已收款
    public Integer updateOrder3(String code,BigDecimal integer) {
        BuyOrder buyOrder0 = orderMapper.selectOrdeCoder(code);
        int id = buyOrder0.getId();
        BigDecimal num = buyOrder0.getrAmount().subtract(integer);
        BuyOrder buyOrder =new BuyOrder();
        buyOrder.setId(id);
        buyOrder.setrAmount(num);
        return orderMapper.updateOrder(buyOrder);
    }


    @Transactional //事务管理
    @Override
    public Integer insertOrder(BuyOrder buyOrder) {
        for(DetailList detailList : buyOrder.getList()){
            detailList.setOrderId(buyOrder.getCode());
            detailListMapper.insertDetailList(detailList);
        }
        return orderMapper.insertOrder(buyOrder);
    }

    @Override
    public BuyOrder selectOrdeCoder(String code) {
        return orderMapper.selectOrdeCoder(code);
    }

    @Override
    public Integer deleteOrder(Integer id) {
        return orderMapper.deleteOrder(id);
    }


    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++分割线---------------------------------------


    @Override
    public List<BuyReturn> selectBuyReturnList(Map<String, Object> map) {
        return buyReturnMapper.selectBuyReturnList(map);
    }

    @Override
    public BuyReturn selectBuyReturn(Integer id) {
        return buyReturnMapper.selectBuyReturn(id);
    }


    @Override
    public Integer updateBuyReturn(BuyReturn buyReturn) {
        return buyReturnMapper.updateBuyReturn(buyReturn);
    }

    @Transactional //事务管理
    @Override
    public Integer insertBuyReturn(BuyReturn buyReturn) {
        for(DetailList detailList:buyReturn.getList()){
            detailList.setOrderId(buyReturn.getCode());
            detailListMapper.insertDetailList(detailList);
        }
        for (DetailList detailList : buyReturn.getList2()){
            detailListMapper.updateDetailList(detailList);
        }
        return buyReturnMapper.insertBuyReturn(buyReturn);
    }

    @Override
    public Integer deleteBuyReturn(Integer id) {
        return buyReturnMapper.deleteBuyReturn(id);
    }


    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++分割线---------------------------------------

    @Override
    public List<ShowWares> selectDetailList(String orderId, Integer type) {
        return detailListMapper.selectDetailList(orderId,type);
    }

    @Override
    public List<ShowWares> selectDetailList2(String orderId, Integer type) {
        return detailListMapper.selectDetailList2(orderId, type);
    }

    @Override
    public Integer insertDetailList(DetailList detailList) {
        return detailListMapper.insertDetailList(detailList);
    }

    @Override
    public Integer updateDetailList(DetailList detailList) {
        return detailListMapper.updateDetailList(detailList);
    }

    @Override
    public Integer deleteDetailList(Integer id) {
        return detailListMapper.deleteDetailList(id);
    }


    @Override
    public Map<String, Object> seeUpdateBuyReturn(Integer id) {
        return buyReturnMapper.seeUpdateBuyReturn(id);
    }

    @Override
    public Map<String, Object> detailListWares(String orderId) {
        return null;
    }


    @Override
    public User orderUser(Integer orderId) {
        int id = orderMapper.selectOrder(orderId).getUserId();
        return userMapper.selectUser(id);
    }

    @Override
    public Integer updateOrderState(Integer orderStaId, Integer recStaId,Integer id) {
        return orderMapper.updateOrderState(orderStaId,recStaId,id);
    }

    @Override
    public Integer deleteOrder2(String code) {
        detailListMapper.deleteDetailList2(code);
        return orderMapper.deleteOrder2(code);
    }

    @Override
    public List<BuyOrder> selectOrderList2(Integer userId) {
        return orderMapper.selectOrderList2(userId);
    }

    @Override
    public Integer updateBuyReturnState(Integer orderSta, Integer receSta, Integer id) {
        return buyReturnMapper.updateBuyReturnState(orderSta, receSta, id);
    }

    @Override
    public BuyReturn selectBuyReturn2(String code) {
        return buyReturnMapper.selectBuyReturn2(code);
    }

    @Transactional //订单已付款,减去作废的已付款
    @Override
    public Integer updateBuyReturn2(String code, BigDecimal bigDecimal) {
        BuyReturn buyReturn1 = buyReturnMapper.selectBuyReturn2(code);
        int id = buyReturn1.getId();
        BigDecimal num = buyReturn1.getrAmount().subtract(bigDecimal);
        BuyReturn buyReturn =new BuyReturn();
        buyReturn.setId(id);
        buyReturn.setrAmount(num);
        return buyReturnMapper.updateBuyReturn(buyReturn);
    }

    @Transactional
    @Override
    public Integer updateDetailList2(List<DetailList> list) {
        try {
            for(DetailList detailList: list){
                detailListMapper.updateDetailList(detailList);
            }
        }catch (Exception e){
            return -1;
        }
        return 1;
    }


}
